﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace Sizebox.CharacterEditor
{
	// Token: 0x02000270 RID: 624
	public class SkeletonEditSkeleton
	{
		// Token: 0x1700019C RID: 412
		// (get) Token: 0x06000DD1 RID: 3537 RVA: 0x0006BE05 File Offset: 0x0006A005
		// (set) Token: 0x06000DD2 RID: 3538 RVA: 0x0006BE0D File Offset: 0x0006A00D
		public List<EditBone> Bones { get; private set; }

		// Token: 0x06000DD3 RID: 3539 RVA: 0x0006BE18 File Offset: 0x0006A018
		public void Initialize(Animator animator)
		{
			this.Bones = new List<EditBone>();
			if (!animator || !animator.isHuman)
			{
				return;
			}
			this.FindLowerBody(animator);
			this.FindSpine(animator);
			this.FindArms(animator);
			this.FindBreasts(animator);
			if (this.head)
			{
				this.Bones.Add(this.head);
			}
			if (this.neck)
			{
				this.Bones.Add(this.neck);
			}
			if (this.leftShoulder)
			{
				this.Bones.Add(this.leftShoulder);
			}
			if (this.leftUpperArm)
			{
				this.Bones.Add(this.leftUpperArm);
			}
			if (this.leftLowerArm)
			{
				this.Bones.Add(this.leftLowerArm);
			}
			if (this.leftHand)
			{
				this.Bones.Add(this.leftHand);
			}
			if (this.rightShoulder)
			{
				this.Bones.Add(this.rightShoulder);
			}
			if (this.rightUpperArm)
			{
				this.Bones.Add(this.rightUpperArm);
			}
			if (this.rightLowerArm)
			{
				this.Bones.Add(this.rightLowerArm);
			}
			if (this.rightHand)
			{
				this.Bones.Add(this.rightHand);
			}
			if (this.leftBreast)
			{
				this.Bones.Add(this.leftBreast);
			}
			if (this.rightBreast)
			{
				this.Bones.Add(this.rightBreast);
			}
			if (this.hips)
			{
				this.Bones.Add(this.hips);
			}
			if (this.lowerSpine)
			{
				this.Bones.Add(this.lowerSpine);
			}
			if (this.midSpine)
			{
				this.Bones.Add(this.midSpine);
			}
			if (this.upperSpine)
			{
				this.Bones.Add(this.upperSpine);
			}
			if (this.leftUpperLeg)
			{
				this.Bones.Add(this.leftUpperLeg);
			}
			if (this.leftLowerLeg)
			{
				this.Bones.Add(this.leftLowerLeg);
			}
			if (this.leftFoot)
			{
				this.Bones.Add(this.leftFoot);
			}
			if (this.rightUpperLeg)
			{
				this.Bones.Add(this.rightUpperLeg);
			}
			if (this.rightLowerLeg)
			{
				this.Bones.Add(this.rightLowerLeg);
			}
			if (this.rightFoot)
			{
				this.Bones.Add(this.rightFoot);
			}
		}

		// Token: 0x06000DD4 RID: 3540 RVA: 0x0006C13C File Offset: 0x0006A33C
		public void GeneratePairings(SkeletonEdit editor)
		{
			editor.CreatePair(this.leftShoulder, this.rightShoulder);
			editor.CreatePair(this.leftUpperArm, this.rightUpperArm);
			editor.CreatePair(this.leftLowerArm, this.rightLowerArm);
			editor.CreatePair(this.leftHand, this.rightHand);
			editor.CreatePair(this.leftUpperLeg, this.rightUpperLeg);
			editor.CreatePair(this.leftLowerLeg, this.rightLowerLeg);
			editor.CreatePair(this.leftFoot, this.rightFoot);
			editor.CreatePair(this.leftBreast, this.rightBreast);
		}

		// Token: 0x06000DD5 RID: 3541 RVA: 0x0006C1DC File Offset: 0x0006A3DC
		private void FindSpine(Animator animator)
		{
			Transform boneTransform = animator.GetBoneTransform(HumanBodyBones.Hips);
			this.hips = SkeletonEditSkeleton.GetEditBone(boneTransform);
			Transform inTransform;
			Transform inTransform2;
			Transform inTransform3;
			this.FindSpineTransforms(animator, out inTransform, out inTransform2, out inTransform3);
			this.lowerSpine = SkeletonEditSkeleton.GetEditBone(inTransform3);
			this.midSpine = SkeletonEditSkeleton.GetEditBone(inTransform2);
			this.upperSpine = SkeletonEditSkeleton.GetEditBone(inTransform);
			Transform boneTransform2 = animator.GetBoneTransform(HumanBodyBones.Neck);
			this.neck = SkeletonEditSkeleton.GetEditBone(boneTransform2);
			Transform boneTransform3 = animator.GetBoneTransform(HumanBodyBones.Head);
			this.head = SkeletonEditSkeleton.GetEditBone(boneTransform3);
		}

		// Token: 0x06000DD6 RID: 3542 RVA: 0x0006C260 File Offset: 0x0006A460
		private void FindSpineTransforms(Animator animator, out Transform upperSpine, out Transform middleSpine, out Transform lowerSpine)
		{
			EditBone componentInParent = animator.GetBoneTransform(HumanBodyBones.Hips).GetComponentInParent<EditBone>();
			upperSpine = null;
			middleSpine = null;
			lowerSpine = null;
			Transform boneTransform;
			if (animator.GetBoneTransform(HumanBodyBones.Neck))
			{
				boneTransform = animator.GetBoneTransform(HumanBodyBones.Neck);
			}
			else
			{
				if (!animator.GetBoneTransform(HumanBodyBones.Head))
				{
					return;
				}
				boneTransform = animator.GetBoneTransform(HumanBodyBones.Head);
			}
			EditBone componentInParent2 = boneTransform.GetComponentInParent<EditBone>();
			EditBone parentBone = componentInParent2.ParentBone;
			int num = 0;
			while (parentBone != componentInParent)
			{
				num++;
				parentBone = parentBone.ParentBone;
			}
			if (num == 2)
			{
				middleSpine = componentInParent2.ParentBone.RealTransform;
				lowerSpine = componentInParent2.ParentBone.ParentBone.RealTransform;
				return;
			}
			if (num == 3)
			{
				upperSpine = componentInParent2.ParentBone.RealTransform;
				middleSpine = componentInParent2.ParentBone.ParentBone.RealTransform;
				lowerSpine = componentInParent2.ParentBone.ParentBone.ParentBone.RealTransform;
			}
			else
			{
				upperSpine = componentInParent2.ParentBone.RealTransform;
				int n = num / 2;
				middleSpine = SkeletonEditSkeleton.GetNthParent(componentInParent2, n).RealTransform;
				lowerSpine = SkeletonEditSkeleton.GetNthParent(componentInParent2, num).RealTransform;
			}
		}

		// Token: 0x06000DD7 RID: 3543 RVA: 0x0006C398 File Offset: 0x0006A598
		private static EditBone GetNthParent(EditBone bone, int n)
		{
			if (n <= 0)
			{
				return bone;
			}
			EditBone parentBone = bone.ParentBone;
			for (int i = 1; i < n; i++)
			{
				parentBone = parentBone.ParentBone;
			}
			return parentBone;
		}

		// Token: 0x06000DD8 RID: 3544 RVA: 0x0006C3D0 File Offset: 0x0006A5D0
		private void FindBreasts(Animator animator)
		{
			Transform transform = null;
			Transform transform2 = null;
			this.FindBreastTransforms(animator.GetBoneTransform(HumanBodyBones.Hips), ref transform, ref transform2);
			if (transform && transform2)
			{
				EditBone editBone = SkeletonEditSkeleton.GetEditBone(transform);
				EditBone editBone2 = SkeletonEditSkeleton.GetEditBone(transform2);
				this.leftBreast = editBone;
				this.rightBreast = editBone2;
			}
		}

		// Token: 0x06000DD9 RID: 3545 RVA: 0x0006C424 File Offset: 0x0006A624
		private void FindBreastTransforms(Transform transform, ref Transform leftBreast, ref Transform rightBreast)
		{
			string text = transform.name.ToLower();
			if (!leftBreast)
			{
				foreach (string value in SkeletonEditSkeleton.LEFT_BREAST_NAMES)
				{
					if (text.Contains(value) && !this.BreastNameContainsDisqualifiers(text))
					{
						leftBreast = transform;
						break;
					}
				}
			}
			if (!rightBreast)
			{
				foreach (string value2 in SkeletonEditSkeleton.RIGHT_BREAST_NAMES)
				{
					if (text.Contains(value2) && !this.BreastNameContainsDisqualifiers(text))
					{
						rightBreast = transform;
						break;
					}
				}
			}
			if (!leftBreast || !rightBreast)
			{
				int childCount = transform.childCount;
				for (int k = 0; k < childCount; k++)
				{
					this.FindBreastTransforms(transform.GetChild(k), ref leftBreast, ref rightBreast);
				}
			}
		}

		// Token: 0x06000DDA RID: 3546 RVA: 0x0006C524 File Offset: 0x0006A724
		private bool BreastNameContainsDisqualifiers(string transformName)
		{
			foreach (string value in SkeletonEditSkeleton.DISQUALIFIERS)
			{
				if (transformName.Contains(value))
				{
					return true;
				}
			}
			return false;
		}

		// Token: 0x06000DDB RID: 3547 RVA: 0x0006C560 File Offset: 0x0006A760
		private void FindArms(Animator animator)
		{
			Transform boneTransform = animator.GetBoneTransform(HumanBodyBones.LeftShoulder);
			Transform boneTransform2 = animator.GetBoneTransform(HumanBodyBones.RightShoulder);
			Transform boneTransform3 = animator.GetBoneTransform(HumanBodyBones.LeftUpperArm);
			Transform boneTransform4 = animator.GetBoneTransform(HumanBodyBones.RightUpperArm);
			Transform boneTransform5 = animator.GetBoneTransform(HumanBodyBones.LeftLowerArm);
			Transform boneTransform6 = animator.GetBoneTransform(HumanBodyBones.RightLowerArm);
			Transform boneTransform7 = animator.GetBoneTransform(HumanBodyBones.LeftHand);
			Transform boneTransform8 = animator.GetBoneTransform(HumanBodyBones.RightHand);
			this.leftShoulder = SkeletonEditSkeleton.GetEditBone(boneTransform);
			this.rightShoulder = SkeletonEditSkeleton.GetEditBone(boneTransform2);
			this.leftUpperArm = SkeletonEditSkeleton.GetEditBone(boneTransform3);
			this.rightUpperArm = SkeletonEditSkeleton.GetEditBone(boneTransform4);
			this.leftLowerArm = SkeletonEditSkeleton.GetEditBone(boneTransform5);
			this.rightLowerArm = SkeletonEditSkeleton.GetEditBone(boneTransform6);
			this.leftHand = SkeletonEditSkeleton.GetEditBone(boneTransform7);
			this.rightHand = SkeletonEditSkeleton.GetEditBone(boneTransform8);
		}

		// Token: 0x06000DDC RID: 3548 RVA: 0x0006C624 File Offset: 0x0006A824
		private void FindLowerBody(Animator animator)
		{
			Transform boneTransform = animator.GetBoneTransform(HumanBodyBones.LeftUpperLeg);
			Transform boneTransform2 = animator.GetBoneTransform(HumanBodyBones.RightUpperLeg);
			Transform boneTransform3 = animator.GetBoneTransform(HumanBodyBones.LeftLowerLeg);
			Transform boneTransform4 = animator.GetBoneTransform(HumanBodyBones.RightLowerLeg);
			Transform boneTransform5 = animator.GetBoneTransform(HumanBodyBones.LeftFoot);
			Transform boneTransform6 = animator.GetBoneTransform(HumanBodyBones.RightFoot);
			if (boneTransform && boneTransform2)
			{
				EditBone editBone = SkeletonEditSkeleton.GetEditBone(boneTransform);
				EditBone editBone2 = SkeletonEditSkeleton.GetEditBone(boneTransform2);
				this.leftUpperLeg = editBone;
				this.rightUpperLeg = editBone2;
			}
			if (boneTransform3 && boneTransform4)
			{
				EditBone editBone = SkeletonEditSkeleton.GetEditBone(boneTransform3);
				EditBone editBone2 = SkeletonEditSkeleton.GetEditBone(boneTransform4);
				this.leftLowerLeg = editBone;
				this.rightLowerLeg = editBone2;
			}
			if (boneTransform5 && boneTransform6)
			{
				EditBone editBone = SkeletonEditSkeleton.GetEditBone(boneTransform5);
				EditBone editBone2 = SkeletonEditSkeleton.GetEditBone(boneTransform6);
				this.leftFoot = editBone;
				this.rightFoot = editBone2;
			}
		}

		// Token: 0x06000DDD RID: 3549 RVA: 0x0006C710 File Offset: 0x0006A910
		private static EditBone GetEditBone(Transform inTransform)
		{
			if (!inTransform)
			{
				return null;
			}
			EditBone componentInParent = inTransform.GetComponentInParent<EditBone>();
			if (!inTransform.GetComponent<EditBone>() && componentInParent && componentInParent.RealTransform == inTransform)
			{
				return componentInParent;
			}
			return null;
		}

		// Token: 0x06000DDE RID: 3550 RVA: 0x0006C760 File Offset: 0x0006A960
		public SkeletonDataMap[] GenerateDataMap()
		{
			List<SkeletonDataMap> list = new List<SkeletonDataMap>();
			if (this.head)
			{
				list.Add(new SkeletonDataMap(this.head.Key, SkeletonEditBones.Head));
			}
			if (this.neck)
			{
				list.Add(new SkeletonDataMap(this.neck.Key, SkeletonEditBones.Neck));
			}
			if (this.leftShoulder)
			{
				list.Add(new SkeletonDataMap(this.leftShoulder.Key, SkeletonEditBones.LeftShoulder));
			}
			if (this.rightShoulder)
			{
				list.Add(new SkeletonDataMap(this.rightShoulder.Key, SkeletonEditBones.RightShoulder));
			}
			if (this.leftUpperArm)
			{
				list.Add(new SkeletonDataMap(this.leftUpperArm.Key, SkeletonEditBones.LeftUpperArm));
			}
			if (this.leftLowerArm)
			{
				list.Add(new SkeletonDataMap(this.leftLowerArm.Key, SkeletonEditBones.LeftLowerArm));
			}
			if (this.leftHand)
			{
				list.Add(new SkeletonDataMap(this.leftHand.Key, SkeletonEditBones.LeftHand));
			}
			if (this.rightUpperArm)
			{
				list.Add(new SkeletonDataMap(this.rightUpperArm.Key, SkeletonEditBones.RightUpperArm));
			}
			if (this.rightLowerArm)
			{
				list.Add(new SkeletonDataMap(this.rightLowerArm.Key, SkeletonEditBones.RightLowerArm));
			}
			if (this.rightHand)
			{
				list.Add(new SkeletonDataMap(this.rightHand.Key, SkeletonEditBones.RightHand));
			}
			if (this.leftUpperLeg)
			{
				list.Add(new SkeletonDataMap(this.leftUpperLeg.Key, SkeletonEditBones.LeftUpperLeg));
			}
			if (this.leftLowerLeg)
			{
				list.Add(new SkeletonDataMap(this.leftLowerLeg.Key, SkeletonEditBones.LeftLowerLeg));
			}
			if (this.leftFoot)
			{
				list.Add(new SkeletonDataMap(this.leftFoot.Key, SkeletonEditBones.LeftFoot));
			}
			if (this.rightUpperLeg)
			{
				list.Add(new SkeletonDataMap(this.rightUpperLeg.Key, SkeletonEditBones.RightUpperLeg));
			}
			if (this.rightLowerLeg)
			{
				list.Add(new SkeletonDataMap(this.rightLowerLeg.Key, SkeletonEditBones.RightLowerLeg));
			}
			if (this.rightFoot)
			{
				list.Add(new SkeletonDataMap(this.rightFoot.Key, SkeletonEditBones.RightFoot));
			}
			if (this.upperSpine)
			{
				list.Add(new SkeletonDataMap(this.upperSpine.Key, SkeletonEditBones.UpperSpine));
			}
			if (this.midSpine)
			{
				list.Add(new SkeletonDataMap(this.midSpine.Key, SkeletonEditBones.MidSpine));
			}
			if (this.lowerSpine)
			{
				list.Add(new SkeletonDataMap(this.lowerSpine.Key, SkeletonEditBones.LowerSpine));
			}
			if (this.hips)
			{
				list.Add(new SkeletonDataMap(this.hips.Key, SkeletonEditBones.Hips));
			}
			if (this.leftBreast)
			{
				list.Add(new SkeletonDataMap(this.leftBreast.Key, SkeletonEditBones.LeftBreast));
			}
			if (this.rightBreast)
			{
				list.Add(new SkeletonDataMap(this.rightBreast.Key, SkeletonEditBones.RightBreast));
			}
			return list.ToArray();
		}

		// Token: 0x06000DDF RID: 3551 RVA: 0x0006CAE4 File Offset: 0x0006ACE4
		public void SetData(SkeletonEditBones bone, string dataId, BoneTransformData transformation)
		{
			switch (bone)
			{
			case SkeletonEditBones.Head:
				if (this.head)
				{
					this.head.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.Neck:
				if (this.neck)
				{
					this.neck.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.UpperSpine:
				if (this.upperSpine)
				{
					this.upperSpine.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.MidSpine:
				if (this.midSpine)
				{
					this.midSpine.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.LowerSpine:
				if (this.lowerSpine)
				{
					this.lowerSpine.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.Hips:
				if (this.hips)
				{
					this.hips.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.LeftUpperArm:
				if (this.leftUpperArm)
				{
					this.leftUpperArm.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.LeftLowerArm:
				if (this.leftLowerArm)
				{
					this.leftLowerArm.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.LeftHand:
				if (this.leftHand)
				{
					this.leftHand.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.RightUpperArm:
				if (this.rightUpperArm)
				{
					this.rightUpperArm.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.RightLowerArm:
				if (this.rightLowerArm)
				{
					this.rightLowerArm.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.RightHand:
				if (this.rightHand)
				{
					this.rightHand.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.LeftUpperLeg:
				if (this.leftUpperLeg)
				{
					this.leftUpperLeg.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.LeftLowerLeg:
				if (this.leftLowerLeg)
				{
					this.leftLowerLeg.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.LeftFoot:
				if (this.leftFoot)
				{
					this.leftFoot.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.RightUpperLeg:
				if (this.rightUpperLeg)
				{
					this.rightUpperLeg.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.RightLowerLeg:
				if (this.rightLowerLeg)
				{
					this.rightLowerLeg.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.RightFoot:
				if (this.rightFoot)
				{
					this.rightFoot.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.LeftBreast:
				if (this.leftBreast)
				{
					this.leftBreast.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.RightBreast:
				if (this.leftBreast)
				{
					this.leftBreast.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.LeftShoulder:
				if (this.leftShoulder)
				{
					this.leftShoulder.SetData(dataId, transformation);
				}
				break;
			case SkeletonEditBones.RightShoulder:
				if (this.rightShoulder)
				{
					this.rightShoulder.SetData(dataId, transformation);
				}
				break;
			}
		}

		// Token: 0x040012B2 RID: 4786
		public EditBone head;

		// Token: 0x040012B3 RID: 4787
		public EditBone neck;

		// Token: 0x040012B4 RID: 4788
		public EditBone leftShoulder;

		// Token: 0x040012B5 RID: 4789
		public EditBone leftUpperArm;

		// Token: 0x040012B6 RID: 4790
		public EditBone leftLowerArm;

		// Token: 0x040012B7 RID: 4791
		public EditBone leftHand;

		// Token: 0x040012B8 RID: 4792
		public EditBone rightShoulder;

		// Token: 0x040012B9 RID: 4793
		public EditBone rightUpperArm;

		// Token: 0x040012BA RID: 4794
		public EditBone rightLowerArm;

		// Token: 0x040012BB RID: 4795
		public EditBone rightHand;

		// Token: 0x040012BC RID: 4796
		public EditBone upperSpine;

		// Token: 0x040012BD RID: 4797
		public EditBone midSpine;

		// Token: 0x040012BE RID: 4798
		public EditBone lowerSpine;

		// Token: 0x040012BF RID: 4799
		public EditBone leftBreast;

		// Token: 0x040012C0 RID: 4800
		public EditBone rightBreast;

		// Token: 0x040012C1 RID: 4801
		public EditBone hips;

		// Token: 0x040012C2 RID: 4802
		public EditBone leftUpperLeg;

		// Token: 0x040012C3 RID: 4803
		public EditBone leftLowerLeg;

		// Token: 0x040012C4 RID: 4804
		public EditBone leftFoot;

		// Token: 0x040012C5 RID: 4805
		public EditBone rightUpperLeg;

		// Token: 0x040012C6 RID: 4806
		public EditBone rightLowerLeg;

		// Token: 0x040012C7 RID: 4807
		public EditBone rightFoot;

		// Token: 0x040012C9 RID: 4809
		private static readonly string[] LEFT_BREAST_NAMES = new string[]
		{
			"left breast",
			"leftbreast",
			"breast left",
			"breastleft",
			"hidarichichi",
			"lpectoral"
		};

		// Token: 0x040012CA RID: 4810
		private static readonly string[] RIGHT_BREAST_NAMES = new string[]
		{
			"right breast",
			"rightbreast",
			"breast right",
			"breastright",
			"migichichi",
			"rpectoral"
		};

		// Token: 0x040012CB RID: 4811
		private static readonly string[] DISQUALIFIERS = new string[]
		{
			"wing",
			"tail",
			"collider",
			"edit bone"
		};
	}
}
